init()



function init() {
    // 初始化显示内容
    $('.cont-item').hide()
    var which = $('.menu-item').index('.menu-item.active')
    $('.cont-item').eq(which).show()

    renderForm()
    prevBind()
    getData()
}

function prevBind() {
    $('.menu-item').click(function() {
        $('.menu-item').removeClass('active')
        $(this).addClass('active')
        $('.cont-item').hide()
        var which = $('.menu-item').index('.menu-item.active')
        $('.cont-item').eq(which).fadeIn()    
    })
    $('.button-search').click(function() {
        getData({
            qstr: $('.input-search').val()
        })
    })
}

function afterBind() {
    $('.button-detail').click(function() {
        var id = $(this).attr('data-num')
        new Dialog({
            width: '50%',
            title: '详情',
            content: $('.detail-'+id)[0]
        })
    })    
}

function renderForm() {
    // 绑定验证和提交
    var myForm = new Form($('#form')[0], {
        avoidSend: function (formData) {
            var that = this
            var form = new FormData()
            form.append('name', $('#name').val())
            form.append('category', $('#category').val())
            form.append('source', $('#source').val())
            form.append('dtype', $('#dtype').val())
            form.append('content', $('#content').val() || '无')
            var files = $('#files')[0].files
            for (var i=0; i<files.length; i++) {
                form.append('files', files[i])
            }
            $('.button-submit').addClass('loading')
            $.ajax({
                url: "/upload",
                type: "POST",
                data: form,
                contentType: false,
                processData: false,
                success: function (res) {
                    if (res.code === 0) {
                        new Dialog().alert('<h6>上传成功</h6>', {
                            type: 'success'
                        })
                        that.element.form.reset()
                        getData()                
                    } else {
                        new Dialog().alert('<h6>上传失败</h6>', {
                            type: 'danger'
                        })
                    }
                },
                error: function () {
                    new Dialog().alert('<h6>上传失败</h6>', {
                        type: 'danger'
                    })
                },
                complete: function () {
                    $('.button-submit').removeClass('loading')
                }
            })
            return true
        }
    })
}

function getData(queryData) {
    $.ajax({
        url: '/data',
        data: queryData,
        success: function (res) {
            if (res.code !== 0) {
                new Dialog().alert('<h6>获取数据失败</h6>', {
                    type: 'danger'
                })
                return                
            }
            $('#table>tbody').html('')
            $('.dialog-cont').html('')
            res.data.forEach(function(item, index) {
                var html = '<tr><td>' 
                    + (index + 1) + '</td><td>'
                    + item[1] + '</td><td>' 
                    + item[2] + '</td><td>'
                    + item[7] + '</td><td>'
                    + item[8] + item[9] + '</td><td>'
                    + '<button class="ui-button button-detail" data-type="primary" data-num="'
                    + item[0] + '">详情</button>'
                    + '<a href="/download/' + item[0] 
                    + '" class="ui-button" data-type="success">下载</a>'
                    +'</td></tr>'
                $('#table>tbody').append(html)
                var cont = '<div class="detail-' +  item[0] + '">'
                item.forEach(function(item1, index) {
                    cont = cont + '<p>' + item1 + '</p>' 
                })
                cont = cont + '</div>'
                $('.dialog-cont').append(cont)
            })
            afterBind()
        }
    })
}